package com.czy.owner.ui.archive;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.czy.owner.R;
import com.czy.owner.adapter.BaseRecyclerAdapter;
import com.czy.owner.adapter.InsuranceCompanyAdapter;
import com.czy.owner.api.GetInsurancesApi;
import com.czy.owner.entity.InsuranceCompanyEntity;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by King on 2017-05-17
 */

public class InsuranceCompanyActivity extends BaseActivity {
    @BindView(R.id.rv_insurance_company)
    RecyclerView rvInsuranceCompany;

    private List<InsuranceCompanyEntity> insuranceCompanyList  = new ArrayList<>();
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_insurance_company;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("保险公司");
        getSubTitle().setVisibility(View.GONE);

    }

    @Override
    protected void updateViews() {
        initData();

    }
    private void initData(){
        GetInsurancesApi mApi = new GetInsurancesApi(new HttpOnNextListener<List<InsuranceCompanyEntity>>() {
            @Override
            public void onNext(List<InsuranceCompanyEntity> o) {
                insuranceCompanyList = o ;
                showData();
            }

            @Override
            public void onCacheNext(String string) {
                super.onCacheNext(string);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<List<InsuranceCompanyEntity>>>() {
                }.getType();
                BaseResultEntity<List<InsuranceCompanyEntity>> cache = gson.fromJson(string, type);
                insuranceCompanyList = cache.getData();
                showData();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
            }
        }, (RxAppCompatActivity) InsuranceCompanyActivity.this);
        HttpManager.getInstance().doHttpDeal(mApi);
    }

    private void showData(){
        InsuranceCompanyAdapter insuranceCompanyAdapter = new InsuranceCompanyAdapter(this,insuranceCompanyList);
        insuranceCompanyAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<InsuranceCompanyEntity>() {
            @Override
            public void onItemClick(int position, InsuranceCompanyEntity o, View view) {
                //Toast.makeText(InsuranceCompanyActivity.this, o.getInsuranceName(), Toast.LENGTH_SHORT).show();
                setResult(RESULT_OK,new Intent().putExtra(Constants.TRANSMISSION_CONTENT_KEY,o));
                finish();
            }
        });
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        rvInsuranceCompany.setLayoutManager(layoutManager);
        rvInsuranceCompany.setAdapter(insuranceCompanyAdapter);
    }
}
